/**
 * 
 */
package d.qapaper.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import d.qapaper.bean.CategoriesInfo;
import d.qapaper.bean.Project;
import d.qapaper.bean.Question;
import d.qapaper.bean.TemplateInfo;
import d.qapaper.bean.Unit;
import d.qapaper.bean.User;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * @author Kaven 2011-4-26
 */
public class SaxReadService {
	//解析的類型 1:問題集  2:template_info 3:categories_info 4:user 5:project_info 6:unit_info
	private int type=1;
	public List<Question> getQuestions(InputStream inputStream,int type) throws Exception {
		this.type=type;
		SAXParserFactory factory=SAXParserFactory.newInstance();  
        SAXParser parser=factory.newSAXParser();  
        QuestionHandle questionHandel=new QuestionHandle();  
        parser.parse(inputStream, questionHandel);  
        return questionHandel.getQuestion();
	}
	
	public List<TemplateInfo> getTemplate(InputStream inputStream,int type) throws Exception {
		this.type=type;
		SAXParserFactory factory=SAXParserFactory.newInstance();  
        SAXParser parser=factory.newSAXParser();  
        QuestionHandle questionHandel=new QuestionHandle();  
        parser.parse(inputStream, questionHandel);  
        return questionHandel.getTemplateList();
	}
	
	public List<CategoriesInfo> getCategoriesInfo(InputStream inputStream,int type) throws Exception {
		this.type=type;
		SAXParserFactory factory=SAXParserFactory.newInstance();  
        SAXParser parser=factory.newSAXParser();  
        QuestionHandle questionHandel=new QuestionHandle();  
        parser.parse(inputStream, questionHandel);  
        return questionHandel.getCategoriesInfos();
	}
	
	public HashMap<String, User> getUserInfo(InputStream inputStream,int type) throws Exception {
		this.type=type;
		SAXParserFactory factory=SAXParserFactory.newInstance();  
        SAXParser parser=factory.newSAXParser();  
        QuestionHandle questionHandel=new QuestionHandle();  
        parser.parse(inputStream, questionHandel);  
        return questionHandel.getUsers();
	}
	
	public List<Project> getProjectInfo(InputStream inputStream,int type) throws Exception {
		this.type=type;
		SAXParserFactory factory=SAXParserFactory.newInstance();  
        SAXParser parser=factory.newSAXParser();  
        QuestionHandle questionHandel=new QuestionHandle();  
        parser.parse(inputStream, questionHandel);  
        return questionHandel.getProjectlist();
	}
	
	public List<Unit> getUnitInfo(InputStream inputStream,int type) throws Exception {
		this.type=type;
		SAXParserFactory factory=SAXParserFactory.newInstance();  
        SAXParser parser=factory.newSAXParser();  
        QuestionHandle questionHandel=new QuestionHandle();  
        parser.parse(inputStream, questionHandel);  
        return questionHandel.getUnitlist();
	}

	private class QuestionHandle extends DefaultHandler {
		private Question question;
		private TemplateInfo template;
		private CategoriesInfo categoriesInfo;
		private User user;
		private Project project;
		private Unit unit;
		private String preTAG;
		private int id=1;
		private List<Question> questionlist;
		private List<TemplateInfo> templatelist;
		private List<CategoriesInfo> categorielist;
		private HashMap<String, User> userlist;
		private List<Project> projectlist;
		private List<Unit> unitlist;
		
		public List<Question> getQuestion() {
			  
			return questionlist;
		}
		public List<TemplateInfo> getTemplateList() {
			  
			return templatelist;
		}
		public List<CategoriesInfo> getCategoriesInfos() {
			  
			return categorielist;
		}
		public HashMap<String, User> getUsers() {
			  
			return userlist;
		}
		

		public List<Project> getProjectlist() {
			return projectlist;
		}
		public List<Unit> getUnitlist() {
			return unitlist;
		}
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (preTAG != null) {
				String data = new String(ch, start, length);
				switch (type) {
				case 1:
					if ("Title".equals(preTAG)) {
						question.setTitle(data);
					} else if ("qAns".equals(preTAG)) {
						question.setQans(data);
					} else if ("Class".equals(preTAG)) {
						question.setType(Integer.parseInt(data));
					}else if("cID".equals(preTAG)){
						question.setCid(data);
					}
					break;
				case 2:
					if ("Title".equals(preTAG)) {
						template.setTitle(data);
					} else if ("tID".equals(preTAG)) {
						template.setTid(data);
					} else if ("SubTitle".equals(preTAG)) {
						template.setSubtitle(data);
					}else if("Feedback".equals(preTAG)){
						template.setFeedback(data);
					}else if("tName".equals(preTAG)){
						template.setName(data);
					}
					break;
				case 3:
					if ("cID".equals(preTAG)) {
						categoriesInfo.setCid(data);
					} else if ("cName".equals(preTAG)) {
						categoriesInfo.setCname(data);
					} 
					break;	
				case 4:
					if ("uname".equals(preTAG)) {
						user.setUname(data);
					} else if ("password".equals(preTAG)) {
						user.setPwd(data);
					} else if ("uID".equals(preTAG)) {
						user.setUid(data);
					} 
					break;	
				case 5:
					if ("pjID".equals(preTAG)) {
						project.setId(data);
					} else if ("pjName".equals(preTAG)) {
						project.setName(data);
					} 
					break;	
				case 6:
					if ("unID".equals(preTAG)) {
						unit.setId(data);
					} else if ("unName".equals(preTAG)) {
						unit.setName(data);
					} 
					break;		
				}
			}
		}
		
		 @Override  
	        public void startElement(String uri, String localName, String qName,  
	                Attributes attributes) throws SAXException  
	        {  
	            if("question".equals(localName))  
	            {  
	                question=new Question();
	                question.setId(id);
	                id++;
	            }else if("template".equals(localName)){
	            	template=new TemplateInfo();
	            }else if ("categories".equals(localName)) {
	            	categoriesInfo=new CategoriesInfo();
				}else if("user".equalsIgnoreCase(localName)){
					user=new User();
				} 
				else if("project".equalsIgnoreCase(localName)){
					project=new Project();
				} 
				else if("unit".equalsIgnoreCase(localName)){
					unit=new Unit();
				} 
	            preTAG=localName;  
	        }  
	          
	        @Override  
	        public void startDocument() throws SAXException  
	        {  
	            questionlist=new ArrayList<Question>();
	            templatelist=new ArrayList<TemplateInfo>();
	            categorielist=new ArrayList<CategoriesInfo>();
	            userlist=new HashMap<String, User>();
	            projectlist=new ArrayList<Project>();
	            unitlist=new ArrayList<Unit>();
	        }  
	          
	        @Override  
	        public void endElement(String uri, String localName, String qName)  
	                throws SAXException  
	        {  
	            if("question".equals(localName))  
	            {  
	                questionlist.add(question);  
	                question=null;  
	            }else if ("template".equals(localName)) {
					templatelist.add(template);
					template=null;
				} else if ("categories".equals(localName)) {
					categorielist.add(categoriesInfo);
					categoriesInfo=null;
				}else if ("user".equals(localName)) {
					userlist.put(user.getUname(),user);
				}else if ("project".equals(localName)) {
					projectlist.add(project);
					project=null;
				}else if ("unit".equals(localName)) {
					unitlist.add(unit);
					unit=null;
				}
	            
	            preTAG=null;  
	        }  
	}
}
